<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;

/**
 * 仓库列表管理
 * Class User
 * @package app\worker\controller
 */
class WareHouse extends Controller
{

    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'WareHouse';

    /**
     * 仓库列表管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '仓库权限管理';
        $query = $this->_query($this->table)
        ->alias('wh')
        ->join('platform p', 'p.id = wh.platform_id', 'left')
        ->field('wh.*,p.name as platform_name')
        ->dateBetween('wh.create_at#create_at')
        ->where(['wh.is_deleted' => '0']);
        if(session('user.ware_house_id')){
            $query->where('wh.id', session('user.ware_house_id')); 
        }
        $query->like('wh.name#name')
        ->equal('wh.status#status')
        ->order('wh.sort desc, wh.create_at desc')
        ->page();
    }

    /**
     * 添加仓库信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }


    /**
     * 修改仓库信息
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$data)
    {
        //获取平台列表
        $this->platform = Db::name('Platform')->where('is_deleted', 0)->where('status', 1)->order('sort desc,number desc')->select();
        if(request()->isPost()){
            if(!isset($data['id'])){
                //获取平台编码
                $platform_number = Db::name('Platform')->where('id', $data['platform_id'])->value('number');
                //自动填写仓库编码
                $number = Db::name($this->table)->where('is_deleted', 0)->where('platform_id', $data['platform_id'])->order('number desc, id desc')->value('number');
                if($number){
                    $data['number'] = sprintf("%06d", $number + 1);
                }else{
                    $data['number'] = $platform_number . '001';
                }
            }
        }
    }

    /**
     * 更改仓库状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function state()
    {
        $this->_save($this->table, ['status' => input('status', '0')]);
    }

    /**
     * 删除仓库
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}
